#
#  Graphics.py
#  Turnpath3d - Modified to run for turnpath
#
#  Created by Pete Lord on 06/03/2009.
#  Copyright (c) 2009 Propellor And Ratchet Games. All rights reserved.
#
import os, pygame

class Graphics(dict):
    
    def __getitem__(self, path):
        try:
            # Try to return the surface(s)
            return dict.__getitem__(self, path)
        except KeyError:
            self.__setitem__(path, self.__load__(path))
            return dict.__getitem__(self, path)
    
    def __load__(self, path):
        return pygame.image.load(path)
"""
class ShipGraphics(Graphics):

    def __load__(self, path):
        return self.__load_image_set__(path, 256), self.__load_image_set__(path, 128), self.__load_image_set__(path, 64), self.__load_icon__(path)
        
        
    def __load_image_set__(self, path, size):
            surface = pygame.image.load(os.path.join(path, str(size) + '.png'))
            
            rect = pygame.Rect((0, 0), (size, size))
            return_list = [[]]
            
            for i in range(8):
                return_list[-1].append([surface.subsurface(rect)])
                rect.move_ip(size, 0)
                
            return_list.append([])
            
            for i in range(8):
                return_list[-1].append([surface.subsurface(rect)])
                rect.move_ip(size, 0)
                
            return return_list
            
    def __load_icon__(self, path):
        return pygame.image.load(os.path.join(path, '32.png'))
"""
        
class ShipGraphics(Graphics):

    def __load__(self, path):
        return self.__load_image_set__(path, 256), self.__load_image_set__(path, 128), self.__load_image_set__(path, 64), self.__load_icon__(path)
        
        
    def __load_image_set__(self, path, size):
            surface = pygame.image.load(os.path.join(path, str(size) + '.png'))
            
            rect = pygame.Rect((0, 0), (size, size))
            return_list = [[]]
            anim_frames = surface.get_height() / size
            
            for i in range(8):
                return_list[-1].append([surface.subsurface(rect)])
                for x in range(1, anim_frames):
                    rect.move_ip(0, size)
                    return_list[-1][-1].append(surface.subsurface(rect))
                    
                if anim_frames == 0:
                    rect.move_ip(size, 0)
                else:
                    rect.move_ip(size, -((anim_frames-1) * size))
                
            return_list.append([])
            
            for i in range(8):
                return_list[-1].append([surface.subsurface(rect)])
                for x in range(1, anim_frames):
                    rect.move_ip(0, size)
                    return_list[-1][-1].append(surface.subsurface(rect))
                    
                if anim_frames == 0:
                    rect.move_ip(size, 0)
                else:
                    rect.move_ip(size, -((anim_frames-1) * size))
            return return_list
            
    def __load_icon__(self, path):
        return pygame.image.load(os.path.join(path, '32.png'))

class WeaponGraphics(ShipGraphics):

    def __load__(self, path):
        return self.__load_image_set__(path, 256), self.__load_image_set__(path, 128), self.__load_image_set__(path, 64)
            
class TileGraphics(Graphics):

    def __load__(self, path):
        return pygame.image.load(os.path.join(path, '256.png')), pygame.image.load(os.path.join(path, '128.png')), pygame.image.load(os.path.join(path, '64.png'))
